package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.pojo.BookConfigBean;
import com.example.demo.pojo.ConfigBean;
import com.example.demo.entity.Info;
import com.example.demo.dao.InfoDao;
import com.example.demo.pojo.HttpResponseBody;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    private static final Logger logger = LogManager.getLogger(TestController.class);
    @Value("${book.name}")
    private String bookName;

    @Value("${book.author}")
    private String bookAuthor;

    @Autowired
    private BookConfigBean bookConfigBean;

    @Autowired
    private ConfigBean configBean;

    @Autowired
    private InfoDao infoDao;

    @GetMapping("test")
    public String test1(){
        return "书名"+bookName + ",作者是"+bookAuthor;
    }

    @GetMapping("test2")
    public BookConfigBean test2(){
        return  bookConfigBean;
    }

    @GetMapping("test3")
    public ConfigBean test3(){
        return  configBean;
    }

    @PostMapping("test4")
    public HttpResponseBody test4(String json){
        HttpResponseBody responseBody = new HttpResponseBody();
        logger.info(json);
        Info info = JSON.parseObject(json.toString(), Info.class);
        infoDao.save(info);
        responseBody.setLogicError(true);
        responseBody.setResponseData("Ok");
        return responseBody;
    }

}
